package com.bluestone.run;

import java.util.ArrayList;

import com.bluestone.context.IContext;

public abstract class BaseController implements IController {
	
	private ArrayList<RunListener> beforeListeners = new ArrayList<RunListener>();
	private ArrayList<RunListener> afterListeners = new ArrayList<RunListener>();

	public boolean execute(IContext context) {
		boolean flag = false;
		executeBefore(context);
		flag = executeContext(context);
		executeAfter(context);
		return flag;

	}
	
	abstract boolean executeContext(IContext context);
	
	void executeBefore(IContext context){
		for(int i=0; i< beforeListeners.size(); i++){
			RunListener listener = beforeListeners.get(i);
			listener.execute(this, context);
			
		}
	}
	
	void executeAfter(IContext context){
		for(int i=0; i< afterListeners.size(); i++){
			RunListener listener = afterListeners.get(i);
			listener.execute(this, context);
			
		}
	}	

	public void addAfterListener(RunListener listener) {
		afterListeners.add(listener);
		
	}

	public void addBeforeListener(RunListener listener) {
		beforeListeners.add(listener);
		
	}
	
    public void stop(IContext context){
    	
    }

}
